var orgData = _appConfigs.getUserData().userOrgs;
$(function(){
	var tableNames = ["dict_gender","dict_education","dict_knry","dict_difficultytype","dict_removereason"];
	for(var i=0;i<tableNames.length;i++){
		var id="#joblessAdd_"+tableNames[i];
		$(id).combobox({
			data:$.appCache.get(tableNames[i]),
			valueField:'code',    
		    textField:'name',
		    editable:false,
		    panelMaxHeight:90,
		});
		
		var id2="#joblessUpdate_"+tableNames[i];
		$(id2).combobox({
			data:$.appCache.get(tableNames[i]),
			valueField:'code',    
		    textField:'name',
		    editable:false,
		    panelMaxHeight:90,
		});
	}
	var streetArr = ['#JoblessSearch_street','#joblessUpdate_dict_street','#joblessAdd_dict_street'];
	for(var i=0;i<streetArr.length;i++){
		$(streetArr[i]).combobox({
			data:$.appCache.get("dict_street"),
			valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    panelMaxHeight:90
		});
	}
	$("#JoblessSearch_street").combobox({
		onSelect: function(record){
	    	$("#JoblessSearch_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
	$("#joblessAdd_dict_street").combobox({
		required:true,
	    onSelect: function(record){
	    	$("#joblessAdd_dict_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    required:true,
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
	$("#joblessUpdate_dict_street").combobox({
		required:true,
		onSelect: function(record){
	    	$("#joblessUpdate_dict_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    required:true,
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	 
	 $("#joblessAdd_dict_removereason").combobox({ 
		 onSelect:function(data){
			 if(data!=null && data!=''){
				 $('#add_removedate').datebox({ 
					 required:true   
			     }); 
			 }
		 }        
	 }); 
	 
	 $("#addJobless").window({ 
		 onClose:function(data){
			 $('#addJoblessForm').form('clear');
			 $('#joblessAdd_removedate').datebox({
				 required:false   
		     });
		 }        
	 });
	
});
/**
 * 默认选街道
 */
if(orgData.porgId == _appConfigs.appOrgId){
	$('#JoblessSearch_street').combobox({disabled:true});
	$('#JoblessSearch_street').combobox('select',orgData.orgId);
	$('#joblessUpdate_dict_street').combobox({readonly:true});
	//$('#joblessUpdate_dict_street').combobox({disabled:true});
	$('#joblessUpdate_dict_street').combobox('select',orgData.orgId);
	$('#importJoblessButton').linkbutton('enable');
}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
	$('#JoblessSearch_street').combobox({disabled:true});
	$('#JoblessSearch_community').combobox({disabled:true});
	$('#JoblessSearch_street').combobox('select',orgData.porgId);
	$('#JoblessSearch_community').combobox('select',orgData.orgId);
	$('#joblessUpdate_dict_street').combobox({readonly:true});
	//$('#joblessUpdate_dict_street').combobox({disabled:true});
	$('#joblessUpdate_dict_community').combobox({readonly:true});
	//$('#joblessUpdate_dict_community').combobox({disabled:true});
	$('#joblessUpdate_dict_street').combobox('select',orgData.porgId);
	$('#joblessUpdate_dict_community').combobox('select',orgData.orgId);
	$('#importJoblessButton').linkbutton('disable');
}else{
	$("#JoblessSearch_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90
	});
	$("#joblessUpdate_dict_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    required:true,
	    panelMaxHeight:90
	}); 
	$('#importJoblessButton').linkbutton('disable');
}
$('#joblessInfoGrid').datagrid({ 
	url:'jobless/management/joblessInfoList',  
	queryParams:{
		street: $('#JoblessSearch_street').combobox('getValue'),
		community: $('#JoblessSearch_community').combobox('getValue')
	},
    idField:'unemployedId',
    rownumbers:true,
    fitColumns:true,
    singleSelect:true,
    pagination:true,
    
    rowStyler: function(index,row){
		if (row.removereason=='1'||row.removereason=='2'||(row.removereason=='3'&&row.approve=='1')){
			return 'background-color:#f2f2f2;color:#000000;';
		}
	},

    columns:[[  
        {field:'unemployedId',title:'主键',hidden:true}, 
        {field:'name',title:'姓名',width:90,align:'center'}, 
        {field:'sex',title:'性别',width:60,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_gender');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},  
        {field:'idcard',title:'身份证号',width:130,align:'center'},
        {field:'phone',title:'联系方式',width:100,align:'center'}, 
        {field:'education',title:'学历',width:80,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_education');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},  
  	    {field:'unemploymentdate',title:'失业时间',width:90,align:'center',
  	    	   formatter:function(data){ 
  	    		   if(data!=null){
  	    			   var df = new Date(data);  
                       return df.format("yyyy-MM-dd");       			   
      		       } else{
      		    	   return null;
      		       }
      	       }},       	   
        {field:'knry',title:'困难人员',width:90,align:'center',
      	       formatter:function(data){
      	    	   if(data==1)
      	    		   return "是";
      	    	   else
      	    		   return "";
     	       }},  
        {field:'trackingtime',title:'跟踪时间',hidden:'true',
        	   formatter:function(data){ 
        		   if(data!=null){
	    			   var df = new Date(data);  
                    return df.format("yyyy-MM-dd");       			   
   		       } else{
   		    	   return null;
   		       }
   	       }},       
        {field:'removereason',title:'减少原因',width:110,align:'center',
   	    	   formatter:function(value){
   	    		   var dict = $.appCache.get('dict_removereason');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                   }
               }},  
   	    {field:'approve',title:'放弃求职审核',width:100,align:'center',
   	        	formatter:function(value,row){
   	        		//alert(row.removereason);
   	        		if(row.removereason!='3'||row.approve==null)
   	        			return '';
   	        		else{
   	        			//alert(row.approve);
   	        			var dict = $.appCache.get('dict_approvestate');
   	                    if(!dict || !dict.length) {
   	                    	return row.approve;
   	                    } else {
   	                    	var i = 0;
   	                        for(i; i<dict.length; i++) {
   	                        	var data = dict[i];
   	                            if(row.approve == data.code) {
   	                            	return data.name;
   	                            }
   	                        }
   	                    }
   	        		}  	        	   
          		   
    	       }},   
        {field:'removedate',title:'减少时间',width:90,align:'center',
     	       formatter:function(value,row){
     	    	   if(row.removereason=='3' && (row.approve=='0'||row.approve==null))
     	    		   return '';
     	    	   else{
     	    		  if(row.removedate!=null){
     	    			  var df = new Date(row.removedate);  
                          return df.format("yyyy-MM-dd");       			   
   		              } else{
   		            	  return null;
   		              }   	    		   
     	    	   }   	    	  
	           }},  
        {field:'street',title:'街道',width:100,align:'center',
	           formatter:function(value){
	        	   var dict = $.appCache.get('dict_street');
	               if(!dict || !dict.length) {
	            	   return value;
	               } else {
	            	   var i = 0;
	                   for(i; i<dict.length; i++) {
	                	   var data = dict[i];
	                       if(value == data.code) {
	                    	   return data.name;
	                       }
	                   }
	               }
	           }},    
        {field:'community',title:'社区',width:120,align:'center',
	           formatter:function(value){
	        	   var dict = $.appCache.get('dict_community');
		           if(!dict || !dict.length) {
		        	   return value;
		           } else {
		        	   var i = 0;
		               for(i; i<dict.length; i++) {
		            	   var data = dict[i];
		                   if(value == data.code) {
		                	   return data.name;
		                   }
		               }
		           }
		       }},    
        {field:'remark',title:'备注',width:80,align:'center'}
    ]]   
}); 

/**
 * 查询条件
 */
function joblessSearch(){
	$('#joblessInfoGrid').datagrid('load',{
		street: $('#JoblessSearch_street').combobox('getValue'),
		community: $('#JoblessSearch_community').combobox('getValue'),
		name: $('#JoblessSearch_name').val(),	
		idcard: $('#JoblessSearch_idCard').val() 					
	});
	$('#joblessInfoGrid').datagrid('unselectAll');
}

function addJobless(){
	if(orgData.porgId == _appConfigs.appOrgId){
		$('#joblessAdd_dict_street').combobox({readonly:true});
		//$('#joblessAdd_dict_street').combobox({disabled:true});
		$('#joblessAdd_dict_street').combobox('select',orgData.orgId);
	}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
		$('#joblessAdd_dict_street').combobox({readonly:true});
		//$('#joblessAdd_dict_street').combobox({disabled:true});
		$('#joblessAdd_dict_community').combobox({readonly:true});
		//$('#joblessAdd_dict_community').combobox({disabled:true});
		$('#joblessAdd_dict_street').combobox('select',orgData.porgId);
		$('#joblessAdd_dict_community').combobox('select',orgData.orgId);
	}else{
		$("#joblessAdd_dict_community").combobox({
			data:$.appCache.get('dict_community'),
			valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    required:true,
		    panelMaxHeight:90
		}); 
	}
	
	var curr_time = new Date();
	var strDate = curr_time.getFullYear()+"-"+(curr_time.getMonth()+1)+"-"+curr_time.getDate();
	$('#joblessAdd_unemploymentdate').datebox('setValue',strDate);	
	$('#joblessAdd_dict_knry').combobox({
		onSelect: function(record){
			//alert(record.code);
			if(record.code == '1'){
				$('#joblessAdd_knrydate').datebox('setValue',strDate);
			} else{
				$('#joblessAdd_knrydate').datebox('setValue','');
			}
		}
	});
	$('#joblessAdd_dict_removereason').combobox({
		onSelect: function(record){
			//alert(record.code);
			if(record.code != ''){
				$('#joblessAdd_removedate').datebox('setValue',strDate);
			} else{
				$('#joblessAdd_removedate').datebox('setValue','');
			}
		}
	});
	
	$('#addJobless').window('open');
}

/**
 * 删除一条失业人员信息记录
 * 
 */
function deleteJobless(){
	var rowData = $('#joblessInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$.messager.confirm('提示','确定要删除吗？',function(r){
			if(r){
				$.ajax({
					url:'jobless/management/deleteJoblessInfo',
					type:'post',
					data:{
						unemployedId:rowData.unemployedId
					},
					success:function(data){
						data =  $.parseJSON(data);
						if(data.success == 'true'){
							$.messager.alert('提示','删除成功','info');
							joblessSearch();
						}else{
							$.messager.alert('提示',data.message,'info');
						}
					}
				});
			}
		});
	}
}

/**
 * 回显失业人员信息
 */
function getJoblessInfo(){
	var rowData = $('#joblessInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var curr_time = new Date();
		var strDate = curr_time.getFullYear()+"-"+(curr_time.getMonth()+1)+"-"+curr_time.getDate();
		$('#joblessUpdate_dict_knry').combobox({
			onSelect: function(record){
				//alert(record.code);
				if(record.code == '1'){
					$('#joblessUpdate_knrydate').datebox('setValue',strDate);
				} else{
					$('#joblessUpdate_knrydate').datebox('setValue','');
				}
			}
		});
		$('#joblessUpdate_dict_removereason').combobox({
			onSelect: function(record){
				//alert(record.code);
				if(record.code != ''){
					$('#joblessUpdate_removedate').datebox('setValue',strDate);
				} else{
					$('#joblessUpdate_removedate').datebox('setValue','');
				}
			}
		});
			
		$('#modifyJobless').window('open');
		$.ajax({
				url:'jobless/management/getJoblessInfo',
				type:'post',
				data:{
					unemployedId:rowData.unemployedId					
				},
				success: function (data) {
					if(data==null){
						$('#modifyJoblessForm').form('clear');
					}else{
						$('#modifyJoblessForm').form('load',data);
					}
		        },
		        error:function(data){
		        	$('#modifyJoblessForm').form('clear');
		        }
			});
		}
}

/**
 * 添加失业人员信息
 */
function insertJobless(){
	var flag = $('#addJoblessForm').form('validate');
	if(flag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'jobless/management/insertJoblessInfo',
		    type:'post',
		    async: false,
		    data:$('#addJoblessForm').serialize(),
		    /*data:{
		    	name: $('#joblessAdd_name').val(),
			    sex: $('#joblessAdd_dict_gender').combobox('getValue'),
			    idcard: $('#joblessAdd_idcard').val(),
			    education: $('#joblessAdd_dict_education').combobox('getValue'),
			    phone: $('#joblessAdd_phone').val(),
			    unemploymentdate: $('#joblessAdd_unemploymentdate').datebox('getValue'),
			    intention: $('#joblessAdd_intention').val(),
			    knry: $('#joblessAdd_dict_knry').combobox('getValue'),
			    knrydate: $('#joblessAdd_knrydate').datebox('getValue'),
			    removereason: $('#joblessAdd_dict_removereason').combobox('getValue'),
			    removedate: $('#joblessAdd_removedate').datebox('getValue'),
			    street: $('#joblessAdd_dict_street').combobox('getValue'),
			    community: $('#joblessAdd_dict_community').combobox('getValue'),
			    remark: $('#joblessAdd_remark').val()		
		    },*/
		    success:function(data){
		    	data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','保存成功','info');
				    $('#addJobless').window('close');
				    $('#addJoblessForm').form('clear');
				    joblessSearch();
			    }else{
			    	$.messager.alert('提示',data.message,'info');
			    }
		    },
		    error:function(data){
		    	alert("ajax发生错误！");	
		    }
	    });
	}
	
}

/**
 * 修改失业人员信息
 * 
 */
function modifyJobess(){
	var modifyFlag = $('#modifyJoblessForm').form('validate');
	if(modifyFlag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'jobless/management/updateJoblessInfo',
			type:'post',
	        async: false,
	        data:$('#modifyJoblessForm').serialize(),
	        success:function(data){
	        	data =  $.parseJSON(data);
		        if(data.success == 'true'){
		        	$.messager.alert('提示','保存成功','info');
			        $('#modifyJobless').window('close');
			        $('#modifyJoblessForm').form('clear');
			        joblessSearch();
		        }else{
		        	$.messager.alert('提示',data.message,'info');

		        }
	        },
	        error:function(data){
	        	alert("ajax发生错误！");	
	        }
        });
	}
}

/**
 * 失业服务台账
 */
function serviceStaff(){
	var rowData = $('#joblessInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		var isExists = window.mainpage.mainTabs.isExists('失业人员服务台账', 'jobless/server/ServiceMenu');
		switch (isExists) {
		    case 0: 
		    	window.mainpage.mainTabs.createTab('失业人员服务台账','jobless/server/ServiceMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 1: 
		    	window.mainpage.mainTabs.createTab('失业人员服务台账','jobless/server/ServiceMenu',
		    		'', true, true, true, true, {name: 'bindData', params: rowData}); break;
		    case 2: 
		    	window.mainpage.mainTabs.jumpTab('失业人员服务台账', {name: 'bindData', params: rowData}); break;
		    default: break;
		}
	}
}
/**
 * 增加失业人员基本信息
 */
function saveJoblessInfo(){
	$('#joblessInfoForm').form('submit', {
		url:'labor/management/insertLaborInfoList',
	success: function(data){
		data =  $.parseJSON(data);
		if(data.success == 'true'){
    		$.messager.alert('消息',"保存成功");
			$('#addJobless').window('close');
			$('#joblessInfoForm').form('clear');
			$('#joblessInfoGrid').datagrid('load');
    	}else{
    		$.messager.alert('消息',data.message);
    	}
	} 
	});
}

/**
 * 日期格式化
 * @param value
 * @param row
 * @returns {String}
 */
function formatterDate(value) {
    var date = new Date(value);
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}

function parserDate(s){
	//alert("parserDate");
	var t = Date.parse(s);
	if (!isNaN(t)){
		return new Date(t);
	} else {
		return new Date("1975-08-01");
	}
	
    /*if(!s) return new Date();
	var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	}else{
		return new Date();
	}*/
	
}

function exportJoblessInfoList(){
	var street,community;
	 if(orgData.porgId == _appConfigs.appOrgId){  //街道
		 street = orgData.orgId;
		 community = "";
	 }else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){ //社区
		 street = "";
		 community = orgData.orgId;
	 }else{  //全部
		 street = "";
		 community = "";
	 }
	 window.location.href ="jobless/excel/exportJoblessInfoList?street="+street+"&community="+community;
}
